import wx.stc

class CodeEditor(wx.stc.StyledTextCtrl):
    def __init__(self, *args, **kwargs):
        super(CodeEditor, self).__init__(*args, **kwargs)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)


    def SmartIndent(self, event):
        print "default smart indent..."

    def GetCallTipText(self, pos):
        '''May be overridden to supply appropriate call tip text'''
        return "this is interesting"

    def ShowCallTip(self, pos):
        '''May be overridden to handle special needs'''
        calltip_text = self.GetCallTipText(pos)
        if calltip_text:
            self.CallTipSetBackground("yellow")
            self.CallTipShow(pos, calltip_text)

    def GetKeywordsForCompletion(self):
        '''May be overridden by the current mode'''
        return None

    def ShowAutoComplete(self):
        keywords = self.GetKeywordsForCompletion()
        if keywords:
            keywords.sort()  # Python sorts are case sensitive
            self.AutoCompSetIgnoreCase(False)  # so this needs to match

            # Images are specified with a appended "?type"
            for i in range(len(kw)):
                if kw[i] in keyword.kwlist:
                    kw[i] = kw[i] + "?1"
            self.AutoCompShow(0, " ".join(kw))

    def OnKeyDown(self, event):
        if self.CallTipActive():
            self.CallTipCancel()
        key = event.GetKeyCode()
        if key == wx.WXK_TAB:
            self.SmartIndent(event)
        elif key == 32 and event.ControlDown():
            if event.ShiftDown():
                self.ShowCallTip()
            else:
                self.ShowAutoComplete()
        else:
            event.Skip()
        
